//
//  Knowledge_SecViewController.m
//  CookGuest
//
//  Created by LiuYanJun on 15/9/12.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "Knowledge_SecViewController.h"
#import "AFN.h"
#import "KnowledgeModel.h"
#import "DEFINE.h"
#import "DataBase.h"

@interface Knowledge_SecViewController ()<UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, assign) BOOL isSelected;
@property (nonatomic, retain) KnowledgeModel *knowledgeModel;
@property (nonatomic , retain)UIView *animation;


@end

@implementation Knowledge_SecViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}


-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = self.getTitle;
    
    
    [[DataBase KnowledgeDataBaseHandle] openKnowledgeDB];
    self.isSelected = NO;
    
    [self AFN];
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.webView.scrollView.bounces = NO;
    [self.view addSubview:self.webView];
    
    
    _xuanZeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _xuanZeButton.frame = CGRectMake(0, 0, 30, 30);
    [_xuanZeButton addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    [_xuanZeButton setBackgroundImage:[UIImage imageNamed:@"sandian.png"] forState:UIControlStateNormal];
    UIBarButtonItem * xuanze = [[UIBarButtonItem alloc]initWithCustomView:_xuanZeButton];
    self.navigationItem.rightBarButtonItem = xuanze;
    
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    [leftButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"backk.png"] forState:UIControlStateNormal];
    UIBarButtonItem * custom = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = custom;
    //    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"share"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(Click)];
    //    NSArray *rightBtnArray = [NSArray arrayWithObjects:shareButton, addButton,nil];
    //    self.navigationItem.rightBarButtonItems = rightBtnArray;
    
    // 加载动画
    [self animationView];
    
    //-----------
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(ADJUSTWIDTH*305, 64, ADJUSTWIDTH*65 , ADJUSTHEIGHT*80) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //    _tableView.backgroundColor = [UIColor orangeColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.hidden = YES;
    [self.view addSubview:_tableView];
    
    //-----------
    
}
//--------

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ADJUSTHEIGHT*40.0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray * arr = @[@"收藏",@"分享"];
    static NSString *cellId =@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.backgroundColor = [UIColor colorWithRed:0.0 green:187.0/255 blue:156.0/255 alpha:1.0];
        
    }
    cell.textLabel.text = [arr objectAtIndex:indexPath.row];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ADJUSTWIDTH*305, ADJUSTWIDTH*39, ADJUSTWIDTH*65 , ADJUSTHEIGHT*1)];
    label.backgroundColor = [UIColor blackColor];
    [cell addSubview:label];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
//--------
#pragma mark - 加载动画
- (void)animationView
{
    self.animation = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 -64)];
    [self.view addSubview:self.animation];
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 65, SCREEN_HEIGHT / 2 -100, SCREEN_WIDTH / 375 * 130, SCREEN_HEIGHT / 667 *130)];
    
    [imageView setBackgroundColor:[UIColor redColor]];
    for (NSInteger i = 1; i < 5; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld" , i]];
        [arr addObject:image];
    }
    [self.animation addSubview:imageView];
    imageView.animationImages = arr;
    imageView.animationRepeatCount = 0;
    imageView.animationDuration = 0.5;
    [imageView startAnimating];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT /2-130, SCREEN_WIDTH / 375 * 100, SCREEN_HEIGHT / 667 * 30)];
    label.text = @"玩命加载中...";
    [self.animation addSubview:label];
    
}



-(void)AFN
{
    _url =[NSString stringWithFormat:@"http://api.xiangha.com/zhishi5/nousInfo?code=%@",self.getCode];
    [AFN urlString:_url getAFNdata:^(id responseObject) {
        
        
        
        
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        
        self.knowledgeModel= [[KnowledgeModel alloc]init];
        self.knowledgeModel.html = [dataDic objectForKey:@"html"];
        NSLog(@"html = %@",self.knowledgeModel.html);
        self.knowledgeModel.title = [dataDic objectForKey:@"title"];
        self.knowledgeModel.img = [dataDic objectForKey:@"img"];
        self.knowledgeModel.content = self.getContent;
        
        
        [self.webView loadHTMLString:self.knowledgeModel.html baseURL:nil];
        [self.animation removeFromSuperview];
    }];
}



#pragma mark-  点击收藏
-(void)addClick
{
    _xuanZeButton.selected = !_xuanZeButton.selected;
    if (_xuanZeButton.selected == YES) {
        
        _tableView.hidden = NO;
        
    }else{
        _tableView.hidden = YES;
    }
    
    
    //    if ([[DataBase KnowledgeDataBaseHandle] selectKnowledge].count == 0) {
    //
    //        _isSelected = YES;
    //    }
    //
    //
    //    for (KnowledgeModel *c in [[DataBase KnowledgeDataBaseHandle] selectKnowledge]) {
    //
    //        if (![c.title isEqualToString:self.knowledgeModel.title]){
    //            _isSelected = YES;
    //
    //        }else {
    //
    //            _isSelected = NO;
    //            break;
    //        }
    //    }
    //
    //    if (_isSelected == YES) {
    //        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"是否收藏美食知识" message:nil delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
    //        [alert show];
    //    }else{
    //        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"您已经收藏过了" message:nil delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
    //        [alert show];
    //    }
    
}
//----------------------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        if ([[DataBase KnowledgeDataBaseHandle] selectKnowledge].count == 0) {
            
            _isSelected = YES;
        }
        
        for (KnowledgeModel *c in [[DataBase KnowledgeDataBaseHandle] selectKnowledge]) {
            
            if (![c.title isEqualToString:self.knowledgeModel.title]){
                _isSelected = YES;
                
            }else {
                
                _isSelected = NO;
                break;
            }
        }
        
        if (_isSelected == YES) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"是否收藏" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"您已经收藏过了" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }else if (indexPath.row == 1)
    {
        
        //注意：分享到微信好友、微信朋友圈、微信收藏、QQ空间、QQ好友、来往好友、来往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要参考各自的集成方法
        //             [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:_youJiPhoto];
        
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"55f6913fe0f55aae3900138e"
                                          shareText:[NSString stringWithFormat:@"%@",_url]
                                         shareImage:[UIImage imageNamed:nil]
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,nil]
                                           delegate:self];
        
        
    }
    
}
//-------------------

#pragma mark-  UIAlaterView
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        
        
    }  else {
        
        if ([[DataBase KnowledgeDataBaseHandle] selectKnowledge].count == 0) {
            
            [[DataBase KnowledgeDataBaseHandle] creatKnowledgeTable];
            
            
            [[DataBase KnowledgeDataBaseHandle] insertKnowledgeInfo:self.knowledgeModel];
            
            
        }
        
        NSInteger count = 0;
        for (KnowledgeModel *coco in [[DataBase KnowledgeDataBaseHandle] selectKnowledge]) {
            
            if (![coco.title isEqualToString:self.knowledgeModel.title]) {
                
                count ++;
                
                if ([[DataBase KnowledgeDataBaseHandle] selectKnowledge].count == count) {
                    
                    [[DataBase KnowledgeDataBaseHandle] insertKnowledgeInfo:self.knowledgeModel];
                }
            }
        }
        
    }
}



-(void)leftClick
{
    [self.navigationController popViewControllerAnimated:YES];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
